package com.lagou.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class C3P0Utils {
    //1.创建连接池对象，C3P0对DataSource接口的实现类
    //指定配置方式创建连接池对象
    public static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");

    //2. 获取连接的方法
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    //3. 释放资源
    public static void close(Connection conn, Statement statement){
        if (conn != null && statement != null){
            try {
                statement.close();
                //归还连接
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //4.释放资源
    public static void close(Connection conn, Statement statement, ResultSet resultSet){
        if (conn!=null && statement != null && resultSet != null){
            try {
                resultSet.close();
                statement.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
